﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HR.Appointments.Service;
using HR.Appointments.Service.ValueObjects;

public partial class TakeVacation : EmployeePage
{
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckCredentials();
        _imgConfirm.Visible = IsLoggedIn;
        if (!IsPostBack)
        {
            
			_fromCalendar.SelectedDate = DateTime.Today;
			_toCalendar.SelectedDate = DateTime.Today;
            ListWorkstations();
        }
    }

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        try
        {
            
            if (_fromCalendar.SelectedDate.CompareTo(DateTime.Today) < 0)
            {
                _lblFromDate.Text = "Ekki hægt að taka frí aftur í tímann";
                return;

            }
            
            if (_toCalendar.SelectedDate < _fromCalendar.SelectedDate)
            {
                _lblFromDate.Text = "Ekki hægt að taka frí aftur í tímann";
                return;
            }

            if (_fromCalendar.SelectedDate == _toCalendar.SelectedDate)
            {
                var from = TimeUnit.Create(_ddlFromHours0.SelectedValue + ":" + _ddlFromMinutes0.SelectedValue);
                var to = TimeUnit.Create(_ddlToHours0.SelectedValue + ":" + _ddlToMinutes0.SelectedValue);
                
                if(from.CompareTo(to) > 0)
                {
                    _lblFromDate.Text = "Ólögleg tímasetning valin";
                    return;
                }

            }

            var vacation = ReservedTimeEntity.Create(
                GetCompanyIdFromCookie(),
                _fromCalendar.SelectedDate,
                _toCalendar.SelectedDate,
                TimeUnit.Create(_ddlFromHours0.Text + ":" + _ddlFromMinutes0.Text),
                TimeUnit.Create(_ddlToHours0.Text + ":" + _ddlToMinutes0.Text),
                false,
                Convert.ToInt32(_ddlWorkstation.SelectedValue));

            Facade.Employee().CreateReservedTime(vacation);


            Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Skráning tókst.');", true);
        }
        catch (SubmitException)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að uppfæra.');", true);
        }

		Server.Transfer("~/DisplayTakeTime.aspx");
    }
    #endregion

    #region Link Transfers
    protected void _ibmHomePage_Click(object sender, ImageClickEventArgs e)
    {
        if (!IsPostBack || (e.X != 0 && e.Y != 0))
        {
            Server.Transfer("~/Company.aspx", true);
        }
    }

    protected void _lbtnCompanyBooking_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/CompanyBooking.aspx", true);
    }
    
	protected void _lbtnConfig_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/Config.aspx", true);
    }

	protected void _lbtnCompanyBooking2_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/CompanyBooking.aspx", true);
	}

    #endregion

    #region Private List Methods
    protected void ListWorkstations()
    {
        _ddlWorkstation.DataSource =
            Facade.Employee().GetWorkstations(GetCompanyIdFromCookie());
        _ddlWorkstation.DataBind();
    }
    
    #endregion
	
}
